package com.k.kppcloud.gateway.util.json.jackson.serializer;

import com.fasterxml.jackson.core.JsonGenerator;
import com.fasterxml.jackson.databind.JsonSerializer;
import com.fasterxml.jackson.databind.SerializerProvider;

import java.io.IOException;
import java.util.Date;

/**
 * 1.全局处理
 * 1.1 添加 自定义序列化类上@JsonComponent 注解
 *     @JsonComponent
 *     public class XXSerializer extends JsonSerializer<Date> {}
 * 1.2 Bean 属性不需要添加任何注解
 *     private Date createTime;
 *
 * 2.局部属性处理
 * 2.1 删除 自定义序列化类上@JsonComponent 注解
 *     public class XXSerializer extends JsonSerializer<Date> {}
 * 2.2 Bean 中属性添加
 *     @JsonSerialize(using = DateToLongSerializer.class)
 *     private Date createTime;</code>
 *
 * 3. 日期时间处理
 *     @JsonFormat(shape = JsonFormat.Shape.STRING, pattern="yyyy-MM-dd HH:mm:ss",timezone = "GMT+8")
 *     @DateTimeFormat(pattern = "yyyy-MM-dd")
 *     private Date createTime
 *
 * @author k
 */
//@JsonComponent  会影响全局的 时间
public class DateToLongSerializer extends JsonSerializer<Date> {
    @Override
    public void serialize(Date date, JsonGenerator jsonGenerator,
                          SerializerProvider serializerProvider) throws IOException {
        jsonGenerator.writeNumber(date.getTime()/1000);
    }
}
